<template>
  <div class="cart">
    <van-nav-bar title="购物车" fixed />
    <div class="cart-box" v-if="list"></div>
    <!-- 购物车开头 -->
    <div class="cart-title">
      <span class="all"
        >共<i>{{ cartTotal || 0 }}</i
        >件商品</span
      >
      <span class="edit" @click="isEdit = !isEdit">
        <van-icon name="edit" />
        编辑
      </span>
    </div>

    <!-- 购物车列表 -->
    <div class="cart-list">
      <div class="cart-item" v-for="item in list" :key="item">
        <van-checkbox icon-size="18" v-model="item.checked"></van-checkbox>
        <div class="show">
          <img :src="item.goods.goods_image" alt="" />
        </div>
        <div class="info">
          <span class="tit text-ellipsis-2">{{ item.goods.goods_name }}</span>
          <span class="bottom">
            <div class="price">
              ¥ <span>{{ item.goods.goods_price_max }}</span>
            </div>
            <div class="count-box">
              <button
                class="minus"
                v-show="item.goods_num > 0"
                @click="calc(item, -1)"
              >
                -
              </button>
              <input
                class="inp"
                :num="item.goods_num"
                :value="item.goods_num"
                type="text"
                readonly
              />
              <button class="add" @click="calc(item, 1)">+</button>
            </div>
          </span>
        </div>
      </div>
    </div>

    <div class="footer-fixed">
      <div class="all-check">
        <input type="checkbox" :checked="checkedAll" @click="setCheckedAll" />
        全选
      </div>

      <div class="all-total">
        <div class="price">
          <span>合计：</span>
          <span
            >¥
            <i class="totalPrice" :price="price">{{
              price.toFixed(2)
            }}</i></span
          >
        </div>
        <div v-if="!isEdit" class="goPay">结算</div>
        <div v-else class="delete" @click="delCarById(item)">删除</div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useRoute } from "vue-router";
import { onMounted, ref, computed, watch } from "vue";
import { CarUpdate, getCartList, cartClear } from "../../api/api";
import { useCommon } from "../../hooks/common";
import { useShopCarStore } from "../../store/data";

let $store = useShopCarStore();
let { gotopage } = useCommon();

const route = useRoute();
// const list = ref<any>(null);
const list: any = ref([]);
const isEdit = ref(false);
const cartTotal = ref(0);
const checkedAll = ref(true);

let setCheckedAll = (e: any) => {
  list.value.forEach((item: any) => {
    item.checked = e.target.checked;
  });
};
watch(
  [list],
  () => {
    checkedAll.value = list.value.every((item: any) => item.checked);
  },
  {
    deep: true,
  }
);

let price = computed(() => {
  let checkedList = list.value.filter((item: any) => item.checked);
  if (checkedList.length) {
    console.log(checkedList);

    let newList = checkedList.map(
      (item: any) => item.goods_num * item.goods.goods_price_max
    );
    return newList.reduce((a: any, b: any) => a + b);
  }
  return 0;
});

const getCartData = async () => {
  const res: any = await getCartList();
  cartTotal.value = res.data.cartTotal;
  list.value = res.data.list.map((item: any) => {
    item.checked = false;
    return item;
  });
};

let calc = async (item: any, num: any) => {
  item.goods_num = item.goods_num + num;
  await CarUpdate({
    goodsId: item.goods.goods_id,
    goodsSkuId: "0",
    goodsNum: item.goods_num,
  });
};

let delCarById = async (item: any) => {
  console.log(item);
  let res: any = await cartClear({
    cartIds: [item.id],
  });
  if (res.status == 200) {
    list.value = list.value.filter((info: any) => info.id !== item.id);
  }
};

onMounted(async () => {
  getCartData();
  list.value = res.data.list.map((item: any) => {
    item.checked = false;
    return item;
  });
});
</script>


<!-- <script>
import { mapGetters, mapState } from "vuex";
import CountBox from "@/components/CountBox.vue";

export default {
  name: "CartPage",
  components: {
    CountBox,
  },
  data() {
    return {
      isEdit: false,
    };
  },
  watch: {
    isEdit(value) {
      if (value) {
        this.$store.commit("cart/toggleAllCheck", false);
      } else {
        this.$store.commit("cart/toggleAllCheck", true);
      }
    },
  },
  computed: {
    mode() {
      return this.$route.query.mode;
    },
    cartIds() {
      return this.$route.query.cartIds;
    },
    ...mapState("cart", ["cartList"]),
    ...mapGetters("cart", [
      "cartTotal",
      "selCount",
      "selPrice",
      "isAllChecked",
      "selCartList",
    ]),
    isLogin() {
      return this.$store.getters.token;
    },
  },

  methods: {
    goPay() {
      if (this.selCount > 0) {
        this.$router.push({
          path: "/pay",
          query: {
            mode: "cart",
            cartIds: this.selCartList.map((item) => item.id).join(","),
          },
        });
      }
    },
    async handleDel() {
      if (this.selCount === 0) return;
      await this.$store.dispatch("cart/delSelect");
      this.isEdit = false;
    },
    changeCount(value, goodsId, skuId) {
      this.$store.dispatch("cart/changeCountAction", {
        value,
        goodsId,
        skuId,
      });
    },
    toggleCheck(goodsId) {
      this.$store.commit("cart/toggleCheck", goodsId);
    },
    toggleAllCheck() {
      this.$store.commit("cart/toggleAllCheck", !this.isAllChecked);
    },
  },
  created() {
    if (this.isLogin) {
      this.$store.dispatch("cart/getCartAction");
    }
  },
};
</script> -->
  
<style lang="scss" scoped>
.cart {
  padding-top: 46px;
  padding-bottom: 100px;
  background-color: #f5f5f5;
  min-height: 100vh;
  .cart-title {
    height: 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px;
    font-size: 14px;
    .all {
      i {
        font-style: normal;
        margin: 0 2px;
        color: #fa2209;
        font-size: 16px;
      }
    }
    .edit {
      .van-icon {
        font-size: 18px;
      }
    }
  }

  .cart-item {
    margin: 0 10px 10px 10px;
    padding: 10px;
    display: flex;
    justify-content: space-between;
    background-color: #ffffff;
    border-radius: 5px;

    .show img {
      width: 100px;
      height: 100px;
    }
    .info {
      width: 210px;
      padding: 10px 5px;
      font-size: 14px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .bottom {
        display: flex;
        justify-content: space-between;
        .price {
          display: flex;
          align-items: flex-end;
          color: #fa2209;
          font-size: 12px;
          span {
            font-size: 16px;
          }
        }
        .count-box {
          display: flex;
          width: 110px;
          .add,
          .minus {
            width: 30px;
            height: 30px;
            outline: none;
            border: none;
          }
          .inp {
            width: 40px;
            height: 30px;
            outline: none;
            border: none;
            background-color: #efefef;
            text-align: center;
            margin: 0 5px;
          }
        }
      }
    }
  }
}

.footer-fixed {
  position: fixed;
  left: 0;
  bottom: 50px;
  height: 50px;
  width: 100%;
  border-bottom: 1px solid #ccc;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px;

  .all-check {
    display: flex;
    align-items: center;
    .van-checkbox {
      margin-right: 5px;
    }
  }

  .all-total {
    display: flex;
    line-height: 36px;
    .price {
      font-size: 14px;
      margin-right: 10px;
      .totalPrice {
        color: #fa2209;
        font-size: 18px;
        font-style: normal;
      }
    }

    .goPay,
    .delete {
      min-width: 100px;
      height: 36px;
      line-height: 36px;
      text-align: center;
      background-color: #fa2f21;
      color: #fff;
      border-radius: 18px;
      &.disabled {
        background-color: #ff9779;
      }
    }
  }
}

.empty-cart {
  padding: 80px 30px;
  img {
    width: 140px;
    height: 92px;
    display: block;
    margin: 0 auto;
  }
  .tips {
    text-align: center;
    color: #666;
    margin: 30px;
  }
  .btn {
    width: 110px;
    height: 32px;
    line-height: 32px;
    text-align: center;
    background-color: #fa2c20;
    border-radius: 16px;
    color: #fff;
    display: block;
    margin: 0 auto;
  }
}
</style>
  